package com.sojson.util;

import java.math.BigInteger;

/**
 * SM工具类
 * 
 * @author liu
 * @date 2022-03-29
 */
public class Util {

    /**
     * 整形转换成网络传输的字节流(字节数组)型数据
     * 
     * @param num
     * @return
     */
    public static byte[] intToBytes(int num) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte)(0xff & (num >> 0));
        bytes[1] = (byte)(0xff & (num >> 8));
        bytes[2] = (byte)(0xff & (num >> 16));
        bytes[3] = (byte)(0xff & (num >> 24));
        return bytes;
    }

    /**
     * 四个字节的字节数据转换成一个整形数据
     * 
     * @param bytes
     * @return
     */
    public static int byteToInt(byte[] bytes) {
        int num = 0;
        int temp;
        temp = (0x000000ff & (bytes[0])) << 0;
        num = num | temp;
        temp = (0x000000ff & (bytes[1])) << 8;
        num = num | temp;
        temp = (0x000000ff & (bytes[2])) << 16;
        num = num | temp;
        temp = (0x000000ff & (bytes[3])) << 24;
        num = num | temp;
        return num;
    }

    /**
     * 四个字节的字节数据转换成一个整形数据
     * 
     * @param num
     * @return
     */
    public static byte[] longToBytes(long num) {
        int eight = 8;
        byte[] bytes = new byte[eight];
        for (int i = 0; i < eight; i++) {
            bytes[i] = (byte)(0xff & (num >> (i * eight)));
        }

        return bytes;
    }

    /**
     * 大数字转换字节流(字节数组)型数据
     * 
     * @param n
     * @return
     */
    public static byte[] byteConvert32Bytes(BigInteger n) {
        byte[] tmpd = null;
        if (n == null) {
            return null;
        }

        int thirtyThree = 33;
        int thirtyTwo = 32;
        if (n.toByteArray().length == thirtyThree) {
            tmpd = new byte[thirtyTwo];
            System.arraycopy(n.toByteArray(), 1, tmpd, 0, thirtyTwo);
        } else if (n.toByteArray().length == thirtyTwo) {
            tmpd = n.toByteArray();
        } else {
            tmpd = new byte[thirtyTwo];
            for (int i = 0; i < thirtyTwo - n.toByteArray().length; i++) {
                tmpd[i] = 0;
            }
            System.arraycopy(n.toByteArray(), 0, tmpd, thirtyTwo - n.toByteArray().length, n.toByteArray().length);
        }
        return tmpd;
    }

    /**
     * 十六进制串转化为byte数组
     * 
     * @param hex
     * @return
     */
    public static byte[] hexToByte(String hex) {
        int two = 2;
        if (hex.length() % two != 0) {
            ExpUtil.throwEx("字符串位数为奇数");
        }
        char[] arr = hex.toCharArray();
        byte[] b = new byte[hex.length() / 2];
        for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
            String swap = "" + arr[i++] + arr[i];
            int byteint = Integer.parseInt(swap, 16) & 0xFF;
            b[j] = new Integer(byteint).byteValue();
        }
        return b;
    }

    /**
     * 字节数组转换为十六进制字符串
     * 
     * @param b
     * @return
     */
    public static String byteToHex(byte[] b) {
        if (b == null) {
            ExpUtil.throwEx("字节数组不存在");
        }

        StringBuilder hs = new StringBuilder();
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0xff);
            if (stmp.length() == 1) {
                hs.append("0").append(stmp);
            } else {
                hs.append(stmp);
            }
        }
        return hs.toString().toUpperCase();
    }

}